package com.msdp.controller;

import com.msdp.dto.Result;
import com.msdp.entity.Message;
import com.msdp.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 添加用户到聊天列表
     * @param receiveUserId
     * @return
     */
    @GetMapping("/chat/{id}")
    public Result chat(@PathVariable("id") Long receiveUserId){
        messageService.chat(receiveUserId);
        return Result.ok();
    }

    /**
     * 发送消息
     * @param message
     * @return
     */
    @PostMapping
    public Result sendMessage(@RequestBody Message message){
        messageService.sendMessage(message);
        return Result.ok();
    }

    /**
     * 根据接收人id获取消息列表
     * @param receiveUserId
     * @return
     */
    @GetMapping("/{receiveUserId}")
    public Result getMessages(@PathVariable("receiveUserId") Long receiveUserId ){
        log.info("id" + receiveUserId);
        List<Message> messages =  messageService.getMessages(receiveUserId);
        return Result.ok(messages);
    }
}
